﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;
using LOGISTCBUS.Entities;

namespace LOGISTCBUS.DAO
{
    public class CustomerTripDAO : BaseDAO
    {
        #region public methods
        public Boolean insert(CustomerTrip ct)
        {
            var db = new Db();
            db.Connect();
            String sql = "insert into customer_trips (id_customer, id_trip, description) ";
            sql += "values (@id_customer, @id_trip, @description);";
            var parameters = new Dictionary<String, Object>();
            parameters["@id_customer"] = ct.Customer.Id;
            parameters["@id_trip"] = ct.Trip.Id;
            parameters["@description"] = ct.Description;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public Boolean update(CustomerTrip ct)
        {
            var db = new Db();
            db.Connect();
            String sql = "update customer_trips set id_customer = @id_customer, id_trip = @id_trip, description = @description where id= @id;";
            var parameters = new Dictionary<String, Object>();
            parameters["@id_customer"] = ct.Customer.Id;
            parameters["@id_trip"] = ct.Trip.Id;
            parameters["@description"] = ct.Description;
            parameters["@id"] = ct.Id;
            try
            {
                var sucess = db.Execute(sql, parameters);
                db.Commit();
                db.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                db.Rollback();
                db.Disconnect();
                return false;
                throw ex;
            }
        }

        public CustomerTrip selectById(Int32 id)
        {
            var db = new Db();
            db.Connect();
            var dt = db.Select("select * from customer_trips where id = " + id);
            db.Disconnect();
            if (dt == null || dt.Rows.Count <= 0)
                return null;
            DataRow r = dt.Rows[0];
            var obj = new CustomerTrip()
            {
                Id = Convert.ToInt32(r["id"]),
                Customer = new Customer() { Id = Convert.ToInt32(r["id_customer"]) },
                Trip = new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                Description = r["description"].ToString(),
                Removed = Convert.ToBoolean(r["removed"])
            };
            return obj;
        }

        public List<CustomerTrip> select()
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CustomerTrip>();
            var dt = db.Select("select * from customer_trips where removed = 0 order by id");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CustomerTrip()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Customer = new Customer() { Id = Convert.ToInt32(r["id_customer"]) },
                    Trip = new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public List<CustomerTrip> select(Int32 page, Int32 pageSize)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CustomerTrip>();
            var sql = "select * from customer_trips where removed = 0 order by id";
            var dt = db.Select(sql);
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CustomerTrip()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Customer = new Customer() { Id = Convert.ToInt32(r["id_customer"]) },
                    Trip = new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst.Skip((page - 1) * pageSize).Take(pageSize).ToList();
        }

        public Int32 getCount()
        {
            var count = 0;
            var db = new Db();
            db.Connect();
            var lst = new List<CustomerTrip>();
            var sql = "select count(*) as qtd from customer_trips where removed = 0";
            var dt = db.Select(sql);
            db.Disconnect();
            if (dt != null && dt.Rows.Count > 0)
            {
                var r = dt.Rows[0];
                count = Convert.ToInt32(r["qtd"]);
            }
            return count;
        }

        public List<CustomerTrip> select(String filter)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CustomerTrip>();
            var dt = db.Select("select * from customer_trips where removed = 0 and " + getConditions(filter, "description") + " order by id");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CustomerTrip()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Customer = new Customer() { Id = Convert.ToInt32(r["id_customer"]) },
                    Trip = new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Description = r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public List<CustomerTrip> selectByTrip(Int32 idTrip)
        {
            var db = new Db();
            db.Connect();
            var lst = new List<CustomerTrip>();
            var dt = db.Select("select * from customer_trips where removed = 0 and id_trip = " + idTrip + " order by id");
            db.Disconnect();
            foreach (DataRow r in dt.Rows)
            {
                lst.Add(new CustomerTrip()
                {
                    Id = Convert.ToInt32(r["id"]),
                    Customer = new Customer() { Id = Convert.ToInt32(r["id_customer"]) },
                    Trip = new Trip() { Id = Convert.ToInt32(r["id_trip"]) },
                    Description = r["description"] is DBNull ? string.Empty : r["description"].ToString(),
                    Removed = Convert.ToBoolean(r["removed"])
                });
            }
            return lst;
        }

        public Boolean delete(CustomerTrip ct)
        {
            var db = new Db();
            db.Connect();
            String sql = "delete from customer_trips where id = @id";
            var parameters = new Dictionary<String, Object>();
            parameters["@id"] = ct.Id;
            var sucess = db.Execute(sql, parameters);
            db.Commit();
            db.Disconnect();
            return sucess;
        }

        public Boolean deleteUpdate(CustomerTrip ct)
        {
            var db = new Db();
            db.Connect();
            String sql = getSQLDeleteUpdate("customer_trips", "id");
            var parameters = new Dictionary<String, Object>();
            parameters["@id"] = ct.Id;
            var sucess = db.Execute(sql, parameters);
            db.Commit();
            db.Disconnect();
            return sucess;
        }

        public Boolean delete(List<CustomerTrip> lst)
        {
            var db = new Db();
            db.Connect();
            foreach (var ct in lst)
            {
                String sql = getSQLDeleteUpdate("customer_trips", "id");
                db.Execute(sql);
            }
            db.Commit();
            db.Disconnect();
            return true;
        }
        #endregion
    }
}